package com.example.service.authService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.authMapper.UserMapper;
import com.example.pojo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @Author xcg
 * @Version 1.0
 */
@Component
public class UserAuthService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LambdaQueryWrapper<LoginUser> eq = new QueryWrapper<LoginUser>().lambda().eq(LoginUser::getLoginCode, username);
        LoginUser loginUser = userMapper.selectOne(eq);
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            return new User(loginUser.getLoginCode(),encoder.encode(loginUser.getPassword()),new ArrayList<>());
//        }else{
//            throw new UsernameNotFoundException("not found the user:"+username);
//        }
    }
}
